package com.linran.wxpublic.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Objects;

public class JSON {
    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static <T> T toJavaObject(String content, Class<T> clz) {
        Objects.requireNonNull(content);
        try {
            return OBJECT_MAPPER.readValue(content, clz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String toJSONString(Object obj) {
        try {
            if (Objects.nonNull(obj)) {
                return OBJECT_MAPPER.writeValueAsString(obj);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
